<?php

class Application_Model_Action extends Engine_Model_Abstract
{
  public function __construct($action_id = null)
  {
    parent::__construct(new Application_Model_DbTable_Actions(), $action_id);
  }


  public function getType()
  {
    return 'action';
  }

  public function getIdentity()
  {
    return $this->action_id;
  }

  public function setPhoto($photo)
  {
    $file = '';
    $fileName = '';
    if( $photo instanceof Zend_Form_Element_File ) {
      $file = $photo->getFileName();
      $fileName = $file;
    } else if( is_array($photo) && !empty($photo['tmp_name']) ) {
      $file = $photo['tmp_name'];
      $fileName = $photo['name'];
    }

    if( !$fileName ) {
      $fileName = $file;
    }

    $extension = ltrim(strrchr(basename($fileName), '.'), '.');
    $base = uniqid()/*rtrim(substr(basename($fileName), 0, strrpos(basename($fileName), '.')), '.')*/;
    $path = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'actions';
    $params = array(
      'parent_type' => $this->getType(),
      'parent_id' => Zend_Auth::getInstance()->getIdentity()->user_id,
      'user_id' => Zend_Auth::getInstance()->getIdentity()->user_id,
      'name' => $base,
      'extension' => $extension,
    );
    @mkdir($path, 0777, true);
    // Resize image (main)
    $mainPath = $path . DIRECTORY_SEPARATOR . $base . '_m.' . $extension;
    $image = Engine_Image::factory();
    $image->open($file)
      ->write($mainPath)
      ->destroy();

    // Resize image (profile)
    $profilePath = $path . DIRECTORY_SEPARATOR . $base . '_p.' . $extension;
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(200, 400)
      ->write($profilePath)
      ->destroy();

    // Resize image (normal)
    $normalPath = $path . DIRECTORY_SEPARATOR . $base . '_in.' . $extension;
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(100, 120)
      ->write($normalPath)
      ->destroy();

    // Resize image (icon)
    $squarePath = $path . DIRECTORY_SEPARATOR . $base . '_is.' . $extension;
    $image = Engine_Image::factory();
    $image->open($file);

    $size = min($image->height, $image->width);
    $x = ($image->width - $size) / 2;
    $y = ($image->height - $size) / 2;

    $image->resample($x, $y, $size, $size, 48, 48)
      ->write($squarePath)
      ->destroy();

    $db = Zend_Db_Table::getDefaultAdapter();
    $select = $db->select();
    $select->from('storage_files', array('file_id'))
      ->where('parent_id = ?' , Zend_Auth::getInstance()->getIdentity()->user_id)
      ->where('user_id = ?' , Zend_Auth::getInstance()->getIdentity()->user_id)
      ->where('parent_type = ?', $this->getType())
      ->where('type = ?', '')
      ->limit(1);

    $file = $db->fetchRow($select);
    $iMain = new Application_Model_Storage($file['file_id']);
    $iMain->fill($params);
    $iMain->bridge($mainPath);
    $iMain->save();

    $files = null;
    if ($file) {
      $profileQ = $db->select();
      $profileQ->from('storage_files', array('file_id', 'type'))
        ->where('parent_file_id = ?', $file['file_id'])
        ->limit(3);
      $files = $db->fetchAll($profileQ);
    }
    $cfiles = null;
    foreach ($files as $item) {
      $cfiles [$item['type']] = $item['file_id'];
    }

    $iProfile = new Application_Model_Storage($cfiles['thumb.profile']);
    $iProfile->fill($params);
    $iProfile->bridge($profilePath, 'thumb.profile');
    $iProfile->setParentFileId($iMain->getFileId());
    $iProfile->save();

    $iIconNormal = new Application_Model_Storage($cfiles['thumb.normal']);
    $iIconNormal->fill($params);
    $iIconNormal->bridge($normalPath, 'thumb.normal');
    $iIconNormal->setParentFileId($iMain->getFileId());
    $iIconNormal->save();

    $iSquare = new Application_Model_Storage($cfiles['thumb.icon']);
    $iSquare->fill($params);
    $iSquare->bridge($squarePath, 'thumb.icon');
    $iSquare->setParentFileId($iMain->getFileId());
    $iSquare->save();

    $this->setPhotoId($iMain->file_id);
    $this->save();
  }

  public function setPhotoId($file_id)
  {
    $this->file_id = $file_id;
  }
}

